package com.hand.demo.app.service.impl;

import com.hand.demo.domain.entity.XzyExample;
import com.hand.demo.domain.repository.XzyExampleRepository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.stream.Collectors;
import io.choerodon.mybatis.domain.AuditDomain;
import java.util.Map;

import org.springframework.stereotype.Service;
import com.hand.demo.app.service.XzyExampleService;
import java.util.List;

import org.hzero.mybatis.helper.UniqueHelper;

/**
 * example表的设计应用服务默认实现
 *
 * @author zeyu.xiao@hand-china.com 2023-09-21 14:51:43
 */
@Service
public class XzyExampleServiceImpl implements XzyExampleService {
                                                
    @Autowired
    private XzyExampleRepository xzyExampleRepository;


    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<XzyExample> batchSave(List<XzyExample> xzyExampleList) {
        Map<AuditDomain.RecordStatus, List<XzyExample>> statusMap = xzyExampleList.stream().collect(Collectors.groupingBy(XzyExample::get_status));
        // 删除
        if (statusMap.containsKey(AuditDomain.RecordStatus.delete)) {
            List<XzyExample> deleteList = statusMap.get(AuditDomain.RecordStatus.delete);
            xzyExampleRepository.batchDeleteByPrimaryKey(deleteList);
        }
        // 更新
        if (statusMap.containsKey(AuditDomain.RecordStatus.update)) {
            List<XzyExample> updateList = statusMap.get(AuditDomain.RecordStatus.update);
            updateList.forEach(item -> {
                // TODO: 唯一性校验
                UniqueHelper.valid(item,XzyExample.XZY_EXAMPLE_U1);
                UniqueHelper.valid(item,XzyExample.XZY_EXAMPLE_U2);
                xzyExampleRepository.updateByPrimaryKeySelective(item);
            });
        }
        // 新增
        if (statusMap.containsKey(AuditDomain.RecordStatus.create)) {
            List<XzyExample> createList = statusMap.get(AuditDomain.RecordStatus.create);
            createList.forEach(item -> {
                // TODO: 唯一性校验
                UniqueHelper.valid(item,XzyExample.XZY_EXAMPLE_U1);
                UniqueHelper.valid(item,XzyExample.XZY_EXAMPLE_U2);
                xzyExampleRepository.insertSelective(item);
            });
        }
        return xzyExampleList;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public XzyExample save(XzyExample xzyExample) {
        //保存example表的设计
        UniqueHelper.valid(xzyExample,XzyExample.XZY_EXAMPLE_U1);
        UniqueHelper.valid(xzyExample,XzyExample.XZY_EXAMPLE_U2);
        if (xzyExample.getId() == null) {
            xzyExampleRepository.insertSelective(xzyExample);
        } else {
            xzyExampleRepository.updateOptional(xzyExample,
                    XzyExample.FIELD_TENANT_ID,
                    XzyExample.FIELD_CODE,
                    XzyExample.FIELD_NAME
            );
        }

        return xzyExample;
    }
}
